package com.pengtu.web.asset.book;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;

import jxl.Sheet;
import jxl.Workbook;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.Attachment;
import com.pengtu.entity.app.Department;
import com.pengtu.entity.asset.book.BkInformation;
import com.pengtu.entity.asset.book.BkSn;
import com.pengtu.service.app.AccountManager;
import com.pengtu.service.asset.BookManager;
import com.pengtu.utils.DateUtils;
import com.pengtu.utils.ExcelHelper;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

@Namespace("/asset/book")
@Results({
		@Result(name = BaseActionSupport.RELOAD, location = "information.action", type = "redirect"),
		@Result(name = "Info", location = "information!input.action?id=${id}", type = "redirect") })
public class InformationAction extends BaseActionSupport<BkInformation> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private BookManager bookmanager;
	private AccountManager accountManager;

	private String id;
	private BkInformation bkInformation;
	private List<Department> deptList;
	private Page<BkInformation> page = new Page<BkInformation>(10);
	private Attachment attachment;

	public Page<BkInformation> getPage() {
		return page;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Override
	public BkInformation getModel() {
		// TODO Auto-generated method stub
		return bkInformation;
	}

	@Action("/asset/book/lend/information")
	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		page = bookmanager.findFuzzyQuery(page, queryFilter);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		deptList = accountManager.getDepartment();
		return INPUT;
	}

	public List<Department> getDeptList() {
		return deptList;
	}

	public void setDeptList(List<Department> deptList) {
		this.deptList = deptList;
	}


	@Override
	public String save() throws Exception {
		Department department = new Department();
		department.setId(Struts2Utils.getParameter("DEPTID"));
		bkInformation.setDepartment(department);
		bookmanager.SaveOrUpdate(bkInformation);
		String mes = "";
		if(StringUtils.isNotEmpty(id)){
			mes = "修改";
		}else{
			mes = "新增";
		}
		addActionMessage(mes+"书--" + bkInformation.getBookName());
		return RELOAD;
	}
	
	/**
	 * 
	 * importassets:
	 * 适用:从Excel导入数据保存
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public String importassets() {

		if (null != attachment) {
			try {
				File file = attachment.getUpfile();
				Workbook book = Workbook.getWorkbook(file);
				// 获得第一个工作表对象
				Sheet sheet = book.getSheet(0);
				int rownum = sheet.getRows(); // 得到行数
				// 得到第一列第一行的单元格
				ExcelHelper excelHelper = ExcelHelper.getInstance();
				for (int i = 2; i < rownum; i++) // 循环进行读写
				{
					if (StringUtils
							.isEmpty(excelHelper.getValueAt(sheet, i, 0)))
						continue;
					BkInformation bkInfo = new BkInformation();
					bkInfo.setSindex(excelHelper.getValueAt(sheet, i, 0));
					bkInfo.setBookType(excelHelper.getValueAt(sheet, i, 1));
					bkInfo.setBookNo(excelHelper.getValueAt(sheet, i, 2));
					bkInfo.setBookName(excelHelper.getValueAt(sheet, i, 3));
					// 出版时间
					try {
						SimpleDateFormat setEffectiveDate = new SimpleDateFormat(
								DateUtils.getDefaultDatePattern());
						if (StringUtils.isNotEmpty(excelHelper.getValueAt(sheet, i,4)))
							bkInfo.setEffectiveDate(setEffectiveDate.parse(excelHelper.getValueAt(
									sheet, i, 4)));
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						bkInfo.setEffectiveDate(null);
					}
					
					bkInfo.setPublisher(excelHelper.getValueAt(sheet, i, 6));
					bkInfo.setStored(excelHelper.getValueAt(sheet, i, 7));
					
//					// 保存部门
//					String depName = excelHelper.getValueAt(sheet, i, 8);
//					Department dept = accountManager.findByDepartment(
//							"depName", depName);
//					if (null != dept)
//						bkInfo.setDepartment(dept);
					bookmanager.SaveOrUpdate(bkInfo);
					
					  //获得书本数量  String.
					if(StringUtils.isEmpty(excelHelper.getValueAt(sheet, i, 5))){
						BkSn bkSn = new BkSn();
						bkSn.setBookinfo(bkInfo);
						bkSn.setBookSN("No1");
						bookmanager.saveBkSn(bkSn);
					}
					try {
						Integer num = Integer.valueOf(excelHelper.getValueAt(sheet, i, 5));
						if(num>1){
					    	for (int j = 0; j < num; j++) {
								BkSn bkSn = new BkSn();
								bkSn.setBookinfo(bkInfo);
								bkSn.setBookSN("No"+j);
								bookmanager.saveBkSn(bkSn);
							}
					    }
					} catch (Exception e) {
						// TODO Auto-generated catch block
						BkSn bkSn = new BkSn();
						bkSn.setBookinfo(bkInfo);
						bkSn.setBookSN("No1");
						bookmanager.saveBkSn(bkSn);
					}
				    
				}
				book.close();
			} catch (Exception e) {
				System.out.println(e);
			}
			return "successMessage";
		}
		return null;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		addActionMessage("删除ID:--" + id);
		bookmanager.deleteBI(id);
		return RELOAD;
	}
	
	/**
	 * 检查图书编号是否唯一
	 */
	public void checkNo() {
		String bookNo = Struts2Utils.getParameter("bookNo");
		String oldBookNo = Struts2Utils.getParameter("oldBookNo");
		if (bookmanager.checkBookNo(bookNo, oldBookNo)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
	}
	/**
	 * 检查图书上架号是否唯一
	 */
	public void checkSindex() {
		String sindex = Struts2Utils.getParameter("sindex");
		String oldSindex = Struts2Utils.getParameter("oldSindex");
		if (bookmanager.checkBookNo(sindex, oldSindex)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
	}

	@Override
	protected void prepareModel() throws Exception {

		if (StringUtils.isNotEmpty(id)) {
			bkInformation = bookmanager.findByBIproperty("id", id);
		} else {
			bkInformation = new BkInformation();
		}
	}

	public Attachment getAttachment() {
		return attachment;
	}

	public void setAttachment(Attachment attachment) {
		this.attachment = attachment;
	}

	@Autowired
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	@Autowired
	public void setBookmanager(BookManager bookmanager) {
		this.bookmanager = bookmanager;
	}

}
